package com.generic.action;

import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.util.ServletContextAware;

import com.generic.dao.impl.ImageDao;
import com.generic.dao.impl.MemberDao;
import com.generic.ds.HibernateSession;
import com.generic.module.impl.Image;
import com.generic.module.impl.Member;
import com.generic.util.Constant;
import com.generic.util.FileUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Namespace("/")
public class Photo extends ActionSupport implements ServletContextAware {

	private static final long serialVersionUID = -7204695240685544625L;
	private static final Log log = LogFactory.getLog(Photo.class);
	private ServletContext servletContext;
	private Long id;
	private List<Image> images;	
	private String ids;
	private String captions;
	private Long defaultId;
	private Long memberId;

	public Long getMemberId() {
		return memberId;
	}

	public void setMemberId(Long memberId) {
		this.memberId = memberId;
	}

	public String getImageFolder() {
		return Constant.IMAGE_THUMB_PATH;
	}
	
	public List<Image> getImages() {
		return images;
	}

	public Long getId() {
		return id;
	}

	public void setImages(List<Image> images) {
		this.images = images;
	}
	
	public String getIds() {
		return ids;
	}
	
	public String getCaptions() {
		return captions;
	}
	
	public Long getDefaultId() {
		return defaultId;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}
	
	public void setCaptions(String test) {
		this.captions = test;
	}

	public void setDefaultId(Long defaultId) {
		this.defaultId = defaultId;
	}

	@Override
	public void setServletContext(ServletContext context) {
		this.servletContext = context;
	}
	
	public String execute() {
		
		HibernateSession hs = new HibernateSession();
		try {
			ImageDao imageDao = new ImageDao(hs);
			images = imageDao.getImages(memberId);
		} catch (Exception e) {
			log.error(e.getMessage());
		} finally {
			hs.closeSession();
		}
		return SUCCESS;
	}
	
	@Action(value = "photo-save", results = {
		    @Result(name = "success", location="ajax-msg.jsp") })
	public String save() {		
		HibernateSession hs = new HibernateSession();
		try {
			hs.beginTransaction();
			
			ImageDao imageDao = new ImageDao(hs);
			String[] ids = this.ids.split(",");
			String[] captions = this.captions.split(",");
			for(int i=0; i<ids.length; i++) {
				Image img = (Image)imageDao.find(Long.parseLong(ids[i]));
				img.setCaption(captions[i]);
				imageDao.insertOrUpdate(img);
			}
			
			hs.commitTransaction();
			addActionMessage("Success!");
		} catch (Exception e) {
			hs.rollbackTransaction(e);
		} finally {
			hs.closeSession();
		}
		return SUCCESS;
	}
	
	@Action(value = "/photo-delete", results = {
		    @Result(name = "success", location="ajax-msg.jsp") })
	public String delete() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		Member member = (Member) session.get(Constant.SESSION_MEMBER);
		Long memberId = member.getId();
//		Long memberId = 1l;
		HibernateSession hs = new HibernateSession();
		try {
			hs.beginTransaction();
			
			ImageDao imageDao = new ImageDao(hs);
			String[] ids = this.ids.split(",");
			for (int i = 0; i < ids.length; i++) {
				Image img = (Image) imageDao.find(Long.parseLong(ids[i]));
				String fileName = img.getFileName();
				/* Delete image meta info from database */
				imageDao.delete(img);
				/* Delete image from file system */
				FileUtil.delete(servletContext.getRealPath("/") + Constant.IMAGE_PATH + memberId + Constant.FILE_SEPARATOR + fileName);
				/* Delete image thumbnail from file system */
				FileUtil.delete(servletContext.getRealPath("/") + Constant.IMAGE_THUMB_PATH + memberId + Constant.FILE_SEPARATOR + fileName);
			}
			
			hs.commitTransaction();
			addActionMessage("Success!");
		} catch (Exception e) {
			hs.rollbackTransaction();
			log.error(e.getMessage());
		} finally {
			hs.closeSession();
		}
		return SUCCESS;
	}
	
	@Action(value = "/photo-default", results = {
		    @Result(name = "success", location="ajax-msg.jsp") })
	public String defaultPhoto() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		Member member = (Member) session.get(Constant.SESSION_MEMBER);
		Long memberId = member.getId();
//		Long memberId = 1l;
		HibernateSession hs = new HibernateSession();
		try {
			hs.beginTransaction();
			
			MemberDao memberDao = new MemberDao(hs);
			memberDao.setDefaultImage(memberId, Long.parseLong(ids));
			
			hs.commitTransaction();
			addActionMessage("Success!");
		} catch (Exception e) {
			hs.rollbackTransaction();
			log.error(e.getMessage());
		} finally {
			hs.closeSession();
		}
		return SUCCESS;
	}
	
	@Action(value = "/photo-gallery")
	public String photoGallery() {
//		Map<String, Object> session = ActionContext.getContext().getSession();
//		Member member = (Member) session.get(Constant.SESSION_MEMBER);
//		Long memberId = member.getId();
//		Long memberId = 1l;
		HibernateSession hs = new HibernateSession();
		try {
			ImageDao imageDao = new ImageDao(hs);			
			images = imageDao.getImages(id);
		} catch (Exception e) {
			hs.rollbackTransaction(e);
		} finally {
			hs.closeSession();
		}
		return SUCCESS;
	}
}
